package cn.itsource.exam.service.impl;

import cn.itsource.exam.domain.Exam;
import cn.itsource.exam.domain.StudentExam;
import cn.itsource.exam.domain.StudentInfo;
import cn.itsource.exam.mapper.ExamMapper;
import cn.itsource.exam.mapper.ExamResultMapper;
import cn.itsource.exam.query.ExamQuery;
import cn.itsource.exam.service.ExamService;
import cn.itsource.exam.util.AjaxResult;
import cn.itsource.exam.util.PageList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    ExamMapper examMapper;

    @Autowired
    ExamResultMapper examResultMapper;
    @Override
    public PageList selectExamList(ExamQuery query) {
        int count = examMapper.countExamList(query);
        if (count == 0){
            return  new PageList<Exam>();
        }
        return new PageList<Exam>(count,examMapper.selectExamList(query));
    }

    @Override
    public List examList() {
        QueryWrapper wrapper = new QueryWrapper();
        return examMapper.selectList(wrapper);
    }

    @Override
    public void updateExam(Exam exam) {
        examMapper.updateById(exam);
    }

    @Override
    public void deleteExam(String id) {
        examMapper.deleteById(id);
    }

    @Override
    public void insertExam(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public List classList() {
        return examMapper.classList();
    }



    @Transactional
    @Override
    public AjaxResult saveExam(Map map) {
        //答辩编号id
//            String selectID = map.get("selectNo").toString();
//            //根据答辩编号id拿到答辩编号
//            String selectNO = examMapper.selectExamNo(selectID);
//            //答辩班级ids
//            JSONArray selectList1 = JSONObject.parseArray(map.get("selectList").toString());
//            if (StringUtils.isNullOrEmpty(selectNO)){
//            }
//            if (StringUtils.isNullOrEmpty(selectNO)){
//
//            }
        //新增答辩信息
        Exam exam=new Exam();
        exam.setCreatetime(new Date());
        exam.setState(0);
        exam.setValue(map.get("examDate").toString());
        examMapper.insert(exam);
        //答辩班级ids
        JSONArray selectList1 = JSONObject.parseArray(map.get("selectList").toString());
        //String examDate = map.get("examDate").toString();
       //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       // Date date = format.parse(examDate);
//        Object[] objects = selectList1.toArray();
        //获取学生list
        List<StudentInfo> list = examMapper.selectStudentByClassIds(selectList1);
        if(list==null||list.size()<1){
            throw new RuntimeException("请先导入班级学生名单");
        }
        Map map1 = new HashMap();
       // map1.put("examno",selectNO);
       // map1.put("examid",selectID);
        map1.put("examid",exam.getId());
        map1.put("examno",exam.getValue());
        map1.put("list",list);
        examMapper.saveExam(map1);
        return AjaxResult.success();

    }

    @Transactional
    @Override
    public AjaxResult confirmExam(Long id) {
        try {
            //修改答辩状态
            examMapper.updateExamStateById(id);
            //查询参与答辩的学生id
           List<Long> ids=examResultMapper.selectByEaxmId(id);
           if(ids!=null){
               for (Long studentExamId : ids) {
                   examResultMapper.updateStateById(studentExamId);
               }
               //设置学生答辩状态为答辩完成
           }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
        return AjaxResult.success();
    }
}
